﻿// Decompiled with JetBrains decompiler
// Type: MCM.Internal.GameFeatures.FileSystemProvider
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using MCM.Abstractions.GameFeatures;
using MCM.Internal.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using TaleWorlds.Library;

#nullable enable
namespace MCM.Internal.GameFeatures
{
  internal sealed class FileSystemProvider : IFileSystemProvider
  {
    private static string EnsureDirectoryEndsInSeparator(string directory)
    {
      return !directory.EndsWith("\\") ? directory + "\\" : directory;
    }

    private IPlatformFileHelper PlatformFileHelper => Common.PlatformFileHelper;

    private static GameDirectory GetConfigsDirectory()
    {
      PlatformDirectoryPath platformDirectoryPath = new PlatformDirectoryPath(PlatformFileType.User, FileSystemProvider.EnsureDirectoryEndsInSeparator("Configs"));
      return new GameDirectory((PlatformDirectoryType) platformDirectoryPath.Type, platformDirectoryPath.Path);
    }

    public GameDirectory GetModSettingsDirectory()
    {
      return this.GetOrCreateDirectory(FileSystemProvider.GetConfigsDirectory(), FileSystemProvider.EnsureDirectoryEndsInSeparator("ModSettings"));
    }

    public GameDirectory? GetDirectory(GameDirectory directory, string name)
    {
      return directory with
      {
        Path = directory.Path + FileSystemProvider.EnsureDirectoryEndsInSeparator(name)
      };
    }

    public GameDirectory GetOrCreateDirectory(GameDirectory directory, string name)
    {
      return directory with
      {
        Path = directory.Path + FileSystemProvider.EnsureDirectoryEndsInSeparator(name)
      };
    }

    public GameFile[] GetFiles(GameDirectory directory, string searchPattern)
    {
      return ((IEnumerable<PlatformFilePath>) this.PlatformFileHelper.GetFiles(new PlatformDirectoryPath((PlatformFileType) directory.Type, directory.Path), searchPattern)).Select<PlatformFilePath, GameFile>((Func<PlatformFilePath, GameFile>) (x => new GameFile(directory, x.FileName))).ToArray<GameFile>();
    }

    public GameFile? GetFile(GameDirectory directory, string fileName)
    {
      return !this.PlatformFileHelper.FileExists(new PlatformFilePath(new PlatformDirectoryPath((PlatformFileType) directory.Type, directory.Path), fileName)) ? (GameFile) null : new GameFile(directory, fileName);
    }

    public GameFile GetOrCreateFile(GameDirectory directory, string fileName)
    {
      PlatformFilePath path = new PlatformFilePath(new PlatformDirectoryPath((PlatformFileType) directory.Type, directory.Path), fileName);
      if (!this.PlatformFileHelper.FileExists(path))
      {
        int num = (int) this.PlatformFileHelper.SaveFile(path, Array.Empty<byte>());
      }
      return new GameFile(directory, fileName);
    }

    public bool WriteData(GameFile file, byte[]? data)
    {
      PlatformFilePath path = new PlatformFilePath(new PlatformDirectoryPath((PlatformFileType) file.Owner.Type, file.Owner.Path), file.Name);
      return data == null ? this.PlatformFileHelper.DeleteFile(path) : this.PlatformFileHelper.SaveFile(path, data) == SaveResult.Success;
    }

    public byte[]? ReadData(GameFile file)
    {
      PlatformFilePath platformFilePath = new PlatformFilePath(new PlatformDirectoryPath((PlatformFileType) file.Owner.Type, file.Owner.Path), file.Name);
      return !this.PlatformFileHelper.FileExists(platformFilePath) ? (byte[]) null : this.PlatformFileHelper.GetFileContent(platformFilePath);
    }

    public string? GetSystemPath(GameFile file)
    {
      return this.PlatformFileHelper.GetFileFullPath(new PlatformFilePath(new PlatformDirectoryPath((PlatformFileType) file.Owner.Type, file.Owner.Path), file.Name));
    }

    public string? GetSystemPath(GameDirectory directory)
    {
      return PlatformFileHelperPCExtended.GetDirectoryFullPath(new PlatformDirectoryPath((PlatformFileType) directory.Type, directory.Path));
    }
  }
}
